﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SubSonic;
using ss = SubSonic;
using SubSonicResearch;
using System.Transactions;

namespace OrmSmackdown.SubSonic
{
    public class Aggregate : IAggregate
    {
        public void InsertRecordsApproachOne(int recordsToInsert)
        {
            using (var scope = new TransactionScope())
            {
                for (var idx = 0; idx < recordsToInsert; ++idx)
                {
                    var perf = new SubSonicResearch.Performance() { MilliSeconds = idx, Action = Constants.STR_AggregateCount, OrmName = "SubSonic22" };
                    perf.Save();
                }

                scope.Complete();
            }
        }

        public void InsertRecordsApproachTwo(int recordsToInsert)
        {
            for (var idx = 0; idx < recordsToInsert; ++idx)
            {
                var perf = new SubSonicResearch.Performance() { MilliSeconds = idx, Action = Constants.STR_AggregateCount, OrmName = "SubSonic22" };
                perf.Save();
            }
        }

        public int GetCountApproachOne()
        {
            var query = new Select(ss.Aggregate.Count(SubSonicResearch.Performance.Columns.PerformanceId))
                .From(SubSonicResearch.Performance.Schema)
                .Where(SubSonicResearch.Performance.Columns.Action).IsEqualTo(Constants.STR_AggregateCount);

            return (int)query.ExecuteScalar();
        }

        public int GetCountApproachTwo()
        {
            var query = new Select(ss.Aggregate.Count(SubSonicResearch.Performance.Columns.PerformanceId))
                .From(SubSonicResearch.Performance.Schema)
                .Where(SubSonicResearch.Performance.Columns.Action).IsEqualTo(Constants.STR_AggregateCount);

            return (int)query.ExecuteScalar();
        }

        public int GetAverageApproachOne()
        {
            var query = new Select(ss.Aggregate.Avg(SubSonicResearch.Performance.Columns.MilliSeconds))
                .From(SubSonicResearch.Performance.Schema)
                .Where(SubSonicResearch.Performance.Columns.Action).IsEqualTo(Constants.STR_AggregateCount);

            return Convert.ToInt32(query.ExecuteScalar());
        }

        public int GetAverageApproachTwo()
        {
            var query = new Select(ss.Aggregate.Avg(SubSonicResearch.Performance.Columns.MilliSeconds))
                .From(SubSonicResearch.Performance.Schema)
                .Where(SubSonicResearch.Performance.Columns.Action).IsEqualTo(Constants.STR_AggregateCount);

            return Convert.ToInt32(query.ExecuteScalar());
        }

        public void DeleteRecordsApproachOne()
        {
            var query = new Delete()
                .From(SubSonicResearch.Performance.Schema)
                .Where(SubSonicResearch.Performance.Columns.Action).IsEqualTo(Constants.STR_AggregateCount);

            query.Execute();
        }

        public void DeleteRecordsApproachTwo()
        {
            AdoNet.Aggregate.ClearAggregateCounts();
        }

        public void Dispose()
        {

        }
    }
}
